package org.javaopenaccess.dao;


import com.db4o.Db4o;
import com.db4o.ObjectContainer;
/**
 * This class is a support class for testing DAO classes.
 * The DAO tests should extend this class and call the
 * appropriate methods when using transaction.
 * @author Miku
 *
 */
public class AbstractDb4oTestSupportDao {
	private ObjectContainer cont;
	/**
	 * This method is should be called before the test case
	 * is doing any transaction specific code.Starts the transaction
	 * and sets it to the DAO class under test. finalizeTransaction() 
	 * should be called, when the test case is over, becouse it closes
	 * the transaction and rolls it back.
	 * @param dao
	 */
	protected void initializeTransaction(AbstractDb4oSupportDao dao){
		//TODO:this should externalized somewhere
		cont = Db4o.openFile("test.db");
		dao.setSession(cont);
		cont.rollback();
	}
	/**
	 * This method closes and rollbacks the transaction started by
	 * initializeTransaction(dao) method.
	 */
	protected void finalizeTransaction(){
		cont.rollback();
		cont.close();
	}
	/**
	 * Returns the session (in db4o, the container) for
	 * for this test object. 
	 */
	protected ObjectContainer getSession(){
		return cont;
	}
}
